﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GnalRit : MonoBehaviour {
    public Transform CheckPoint;
    private float ScreenInWidth;
    public GameObject LastRoom;

    //可用房间
    public GameObject[] Rooms;
    private void Awake()
    {
        ScreenInWidth = Camera.main.orthographicSize * Camera.main.aspect * 2;
        Debug.Log(ScreenInWidth);
    }
    // Use this for initialization
    void Start()
    {
        CheckPoint.localPosition = new Vector3(ScreenInWidth, 0, 0);//子物体相对父物体的位置
    }

    // Update is called once per frame
    void Update()
    {
        //CheckPoint.position=new Vector3 (ScreenInWidth,0,0);
        GenRooms();
    }
    void GenRooms()
    {
        float roomWidth = LastRoom.transform.Find("floor").localScale.x;
        //Debug.Log(roomWidth);
        float EndX = LastRoom.transform.position.x + 0.5f * roomWidth;
        //float roomWidth = LastRoom
        //  .transform.Find("floor")
        //  .localScale.x;
        //float EndX = LastRoom.transform.position.x + 0.5f * roomWidth;

        //当检查点越过了房间的右边界
        if (CheckPoint.position.x > EndX)
        {
            //Debug.Log("需要添加房间了...");
            //随机抽一个房间序号，
            int ind = Random.Range(0, Rooms.Length - 1);
            //动态生成房间
            GameObject newRoom = Instantiate(Rooms[ind]);
            //新房间宽度
            float newRoomWidth = newRoom.transform.Find("floor").localScale.x;
            //新房间位置
            newRoom.transform.position = new Vector3(newRoomWidth * 0.5f + EndX, 0, 0);
            //重新设置
            LastRoom = newRoom;
        }
    }

}
